<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 00:33:05
 * @LastEditTime: 2023-08-03 13:30:20
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Models;

use App\Traits\MaterialTrait;
use App\Traits\SoftDeletesTrait;


class MarketOrderDetails extends BaseModel
    {
    use SoftDeletesTrait;
    use MaterialTrait;
    public $timestamps = true;
    public $table = "sys_market_order_details";
    protected $guarded = [];
    protected $casts = [
        'auxiliary_quantity' => 'decimal:2',
        'quantity'           => 'decimal:2',
        'rate'               => 'decimal:2',
        'tax_price'          => 'decimal:2',
        'price'              => 'decimal:2',
        'tax_amount'         => 'decimal:2',
        'amount'             => 'decimal:2',
        'tax'                => 'decimal:2',
        'plan_quantity'      => 'decimal:2',
    ];
    public function status()
        {
        return $this->belongsTo(Dic::class, "status_id", "key")->where(function ($q)
            {
            $q->where("code", 'OrderStatus');
            });
        }
    public function _creating($model)
        {
        $model->color_material_code = $model->color_id . "_" . $model->material_id;
        }
    public function _updating($model)
        {
        $model->color_material_code = $model->color_id . "_" . $model->material_id;
        }
    public function main()
        {
        return $this->belongsTo(MarketOrder::class, "main_id");
        }
    public function process_route()
        {
        return $this->belongsTo(ProProcessRoute::class);
        }
    public function process()
        {
        return $this->belongsTo(ProProcesses::class);
        }
    public function emergency()
        {
        return $this->belongsTo(Dic::class, "emergency_id", "key")->where(function ($q)
            {
            $q->where("code", 'Emergency');
            });
        }
    }
